package com.atguigu.gulimall.order.service.impl;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.entity.OrderReturnReasonEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.order.dao.OrderItemDao;
import com.atguigu.gulimall.order.entity.OrderItemEntity;
import com.atguigu.gulimall.order.service.OrderItemService;

@RabbitListener(queues = {"hello-java-queue"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * queues声明需要监听的所有队列
     * 类型：class org.springframework.amqp.core.Message
     *
     * 参数可以写以下类型
     * 1、Message message ：原生消息详细信息，头+体
     * 2、T<发送的消息的类型> OrderReturnReasonEntity content  ： 这种不用我们自己转化，spring会自动转化
     * 3、Channel channel：当前传输数据的通道
     *
     * Queue:可以很多人都来监听，只要收到消息，队列删除消息，而且只能有一个收到此消息
     * 场景：
     *      1）、订单服务启动多个；同一个消息，只能有一个客户端收到
     *      2）、只有一个消息完全处理完，方法运行结束，我们就可以接收到下一个消息
     */
//    @RabbitListener(queues = {"hello-java-queue"})
    @RabbitHandler
//    public void receiveMessage(Message message){
//    public void receiveMessage(Message message, OrderReturnReasonEntity content){
    public void receiveMessage(Message message, OrderReturnReasonEntity content, Channel channel){

//        接收到消息...内容...(Body:'{"id":1,"name":"哈哈","sort":null,"status":null,"createTime":1617964311791}' MessageProperties [headers={__TypeId__=com.atguigu.gulimall.order.entity.OrderReturnReasonEntity}, contentType=application/json, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=hello-java-exchange, receivedRoutingKey=hello.java, deliveryTag=1, consumerTag=amq.ctag-ndLWx5cQVQGTAJWAB4rlhg, consumerQueue=hello-java-queue])==> 类型 class org.springframework.amqp.core.Message
        //拿到消息体的内容
        byte[] body = message.getBody();
        //拿到消息头的属性信息
        MessageProperties messageProperties = message.getMessageProperties();
//        System.out.println("接收到消息...内容..." + message + "==> 类型 "+ message.getClass());
        System.out.println("接收到消息...内容..." + message + "==> 内容 "+ content);
        //channerl内按顺序自增的一个id
        long deliveryTag = messageProperties.getDeliveryTag();
        //签收货物,非批量模式
        try {
            //模拟未签收
            if(deliveryTag%2 == 0 ){
                //收货
                channel.basicAck(deliveryTag,false);
                System.out.println("签收了货物..."+deliveryTag);
            }else{
                //退货
                //void basicNack(long var1, boolean var3, boolean var4)  var4 为是否发回服务器重新入队发送
                channel.basicNack(deliveryTag,false,false);
//                channel.basicReject();
                System.out.println("没有签收货物..."+deliveryTag);
            }
        } catch (IOException e) {
            //网络中断
            e.printStackTrace();
        }
    }

    //@RabbitHandler意义在于接收不同类型的消息
    @RabbitHandler
    public void receiveMessage2( OrderEntity content){
        System.out.println("接收到消息...内容..." + content);
    }
}